package org.yjc.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yjc.custom.JqgridParam;
import org.yjc.custom.JqgridUtils;
import org.yjc.entity.EndUser;
import org.yjc.entity.LesseeAdmin;
import org.yjc.entity.Log;
import org.yjc.entity.SysRole;
import org.yjc.entity.SysUser;
import org.yjc.form.EndUserForm;
import org.yjc.form.LesseeAdminForm;
import org.yjc.repository.SysRoleRepository;
import org.yjc.security.UserUtils;
import org.yjc.service.LesseeAdminService;
import org.yjc.service.LogService;

@Controller
@RequestMapping(value="/lesseeadmin")
public class LesseeAdminController {
	@Autowired
	private LesseeAdminService lesseeadminservice;
	@Autowired
	private UserUtils userutils;
	@Autowired 
	private LogService logservice;
	@Autowired
	SysRoleRepository sysroleDAO;
	
	@RequestMapping(value="/lesseeadminmain")
	public String lesseeadmin() {
		return "lesseeadmin/lesseeadminmain";
	}
	
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public Object list(JqgridParam param) {
		Pageable pageable = param.buildPageable();
		Page<LesseeAdmin> page = null;
		if(!param.getSearch()) {
			page=lesseeadminservice.findAll(pageable);
		}else {
			page=lesseeadminservice.findAll(param.buildSpec(), pageable);
		}
		HashMap<String, Object> result = JqgridUtils.getPageResult(page);
		return result;
	}
	
	@RequestMapping(value="/edit")
	@ResponseBody
	public Object edit (String id,LesseeAdminForm form) {
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder(); //密码加密器
		String oper = form.getOper(); //url判断
		LesseeAdmin lesseeadmin = new LesseeAdmin(); 
		if("del".equals(oper)) {
			lesseeadminservice.deleteById(Integer.parseInt(id));
		}else {
			if("edit".equals(oper)) {
				lesseeadmin = lesseeadminservice.findById(Integer.parseInt(id));
			}
			BeanUtils.copyProperties(form, lesseeadmin,"id");
			lesseeadmin.setPassword(encoder.encode("111111")); //密码进行不可逆加密
			SysRole role = sysroleDAO.findById(2).get(); //添加权限
			lesseeadmin.getRoles().add(role);
			SysUser user = userutils.getUser();	 //获取当前用户
			Log log = new Log();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			log.setName(user.getUsername());
			log.setOperationTime(df.format(new Date())); //获取当前系统时间
			log.setOperatiobName("新增租户管理员");
			logservice.save(log);
			lesseeadminservice.save(lesseeadmin);
		}
		return "OK";
	}
	
	@RequestMapping(value="/changepwd")
	@ResponseBody
	public Object changepwd(Integer id,ModelMap map) {
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder(); //密码加密器
		LesseeAdmin lesseeadmin= lesseeadminservice.findById(id);
		lesseeadmin.setPassword(encoder.encode("111111"));
		lesseeadminservice.save(lesseeadmin);
		SysUser user = userutils.getUser();	 //获取当前用户
		Log log = new Log();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		log.setName(user.getUsername());
		log.setOperationTime(df.format(new Date())); //获取当前系统时间
		log.setOperatiobName("重置租户管理员密码");
		logservice.save(log);
		return "重置密码成功";
	}
	
}
